package com.leviassaf.androidbasics;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.transition.Visibility;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class InternalData extends Activity implements OnClickListener {

	EditText edt;
	TextView txtvwSharedPrefText;
	FileOutputStream fileOutputStream;
	final String STR_FILE_NAME = "InternalString";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharedpreferences);
		setJavaRefs();
		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		Button btnSave = (Button)findViewById(R.id.btnSave);
		Button btnLoad = (Button)findViewById(R.id.btnLoad);
		Button btnLoadAllSharedPrefs = (Button)findViewById(R.id.btnLoadAllSharedPrefs);
		Button btnClearSharedPrefs = (Button)findViewById(R.id.btnClearSharedPrefs);
		edt = (EditText)findViewById(R.id.edtSharedText);
		txtvwSharedPrefText = (TextView)findViewById(R.id.txtDataResults);
		btnSave.setOnClickListener(this);
		btnLoad.setOnClickListener(this);
		btnLoadAllSharedPrefs.setOnClickListener(this);
		btnClearSharedPrefs.setOnClickListener(this);
		
		try {
			fileOutputStream = openFileOutput(STR_FILE_NAME, Context.MODE_APPEND);
			fileOutputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int intKey;
		switch (v.getId()){
		case R.id.btnSave:
			String strData = edt.getText().toString();
			/*
			File file = new File(STR_FILE_NAME);
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.close();
			*/
			try {
				fileOutputStream = openFileOutput(STR_FILE_NAME, Context.MODE_PRIVATE);
				fileOutputStream.write(strData.getBytes());
				fileOutputStream.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.btnLoad:
			new LoadSomeStuff().execute();
			break;
		case R.id.btnLoadAllSharedPrefs:
			break;
		case R.id.btnClearSharedPrefs:
			break;
		}
	}

	
	//102
	public class LoadSomeStuff extends AsyncTask<String, Integer, String> {
		
		ProgressDialog progressDialog;
		
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			progressDialog = new ProgressDialog(InternalData.this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMax(100);
			progressDialog.show();
		}
		
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String strDataRead = null;
			FileInputStream fileInputStream = null;
			
			//103
//			for (int i = 0; i < 20; i++){
//				//invoke onProgressUpdate
//				publishProgress(5);
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
			//103
			try {
				fileInputStream = openFileInput(STR_FILE_NAME);
				byte[] arrByteData = new byte[fileInputStream.available()];
				
				int intbufferSize;
				int intmaxBufferSize = 1024;
				final int intFileLength = arrByteData.length;
				long lngTotal = 0;
				int intCount;
				intbufferSize = Math.min(fileInputStream.available(), intmaxBufferSize);
				
				while ((intCount = fileInputStream.read(arrByteData, 0, intbufferSize)) != -1){
					lngTotal += intCount;
					publishProgress((int)(lngTotal * 100 / intFileLength));
					strDataRead = new String(arrByteData);
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				try {
					fileInputStream.close();
					return strDataRead;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			progressDialog.incrementProgressBy(values[0]);
			progressDialog.show();
			
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			txtvwSharedPrefText.setText(result);
			txtvwSharedPrefText.setVisibility(View.VISIBLE);
			progressDialog.dismiss();
		}


		
	
	}
}
